using TEngine;
using UnityEngine;

using System.Collections;
using System.Collections.Generic;
namespace GameLogic
{
	[Window(UILayer.Tips)]
	public partial class Panel_FloatTips : UIWindow
	{
		private int _maxTipsCount = 10;
		private List<Widget_FloatTips> _list;
		protected override void OnRefresh()
		{
			_list = new ();
		}

		protected override void RegisterEvent()
		{
			GameEvent.AddEventListener<string,bool>(LogicEventType.ShowTips, ShowTips);
			GameEvent.AddEventListener<string,bool, string[]>(LogicEventType.ShowTips2, ShowTips);
		}

		#region 全局消息Tips
		private float timer = -0.5f;
		private void ShowTips(string tips, bool isKey = false)
		{
			if (GameTime.time - timer < 0.5f)
			{
				return;
			}
			timer = GameTime.time;
			var item = GetTipsItem();
			item.Open(tips,isKey);
		}

		private void ShowTips(string tips, bool isKey = false, params string[] userDatas)
		{
			if (GameTime.time - timer < 0.5f)
			{
				return;
			}
			timer = GameTime.time;
			var item = GetTipsItem();
			item.Open(tips,isKey,userDatas);
		}
		private Widget_FloatTips GetTipsItem()
		{
			foreach (var tips in _list)
			{
				if (tips.isCanUse)
				{
					return tips;
				}
			}
			if (_list.Count >= _maxTipsCount) return null;
			var floatTips = CreateWidgetByType<Widget_FloatTips>(mNode);
			_list.Add(floatTips);
			return floatTips;
		}
		

		#endregion
		protected override void Close()
		{
			base.Close();
		}

		protected override void OnDestroy()
		{
			base.OnDestroy();
			GameEvent.RemoveEventListener<string,bool>(LogicEventType.ShowTips, ShowTips);
			GameEvent.RemoveEventListener<string,bool, string[]>(LogicEventType.ShowTips2, ShowTips);
		}
	}
}
